<template>
  <div id="menu_id">
    <div class="menu_top">
      <p class="menu_note">
        注：<br />
        1.增加新的栏目为当前栏目下一行<br />
        2.修改完栏目的一行需保存后提交审批才能生效
      </p>
      <div class="menu_add">
        <el-button size="mini" round @click="openPanel">增加</el-button>
      </div>
    </div>
    <div>
      <el-table
        :data="menuList"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column prop="title" label="栏目名称" width="180">
        </el-table-column>
        <el-table-column prop="icon" label="栏目图标" v-slot="$props">
          <template>
            <i :class="$props.row.icon"></i>
          </template>
        </el-table-column>
        <el-table-column prop="url" label="栏目地址"> </el-table-column>
        <el-table-column prop="status" label="状态" v-slot="$props">
          <template>
            <el-button
              v-if="$props.row.status === 0"
              size="mini"
              type="danger"
              plain
              round
              >已禁用</el-button
            >
            <el-button
              v-else-if="$props.row.status === 1"
              size="mini"
              type="success"
              plain
              round
              >已启用</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" v-slot="$props">
          <template>
            <el-button type="primary" plain size="mini" round @click="updateMenu($props.row)"
              >修改</el-button
            >
          </template>
          <template>
            <el-button type="danger" plain size="mini" round @click="delMenu($props.row.id)" 
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addmenu
      ref="menu_ref"
      :isShow="isShow"
      :menuList="menuList"
      @reload_list="reload_list"
    >
    </addmenu>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import addmenu from "../components/addMenu.vue";
export default {
  data() {
    return {
      isShow: { flag: false },
    };
  },

  created() {
    this.MenuListAction();
  },
  methods: {
    Del(id){
      this.$http.post('/api/menudelete',{id}).then((res)=>{
        if(res.code === 200) {
          this.MenuListAction();
        }
      })
    },
    delMenu(id) {
      this.$confirm("确定要删除菜单吗？", "删除菜单", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      }).then(() => {
        this.Del(id);
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      }).catch(()=>{
        this.$message({
            type: "info",
            message: "已取消删除",
          });
      })
    },
    updateMenu(obj) {
      this.isShow.flag = true;

      this.$refs.menu_ref.setFome(obj);
    },
    reload_list() {
      this.MenuListAction();
    },
    openPanel() {
      this.isShow.flag = true;
    },
    ...mapActions("MenuStore", ["MenuListAction"]),
  },
  computed: {
    ...mapState({ menuList: (state) => state.MenuStore.menuList }),
  },
  components: {
    addmenu,
  },
};
</script>

<style>
#menu_id .menu_top {
  display: flex;
  justify-content: space-between;
}
#menu_id .menu_note {
  text-align: left;
  line-height: 16px;
  font-size: 12px;
  color: #c4c4c4;
}
#menu_id .menu_add button {
  display: block;
  width: 60px;
  color: #ff5d3e;
  background-color: #fff;
  border: 1px solid #ff5d3e;
}
</style>